﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.IO;

namespace PacMan
{
     class LeaderboardManager
    {

        private string _fullXmlFileName;
        private bool _isFirstInitialization;

        private XDocument document;
        
        public LeaderboardManager()
        {
            _fullXmlFileName = Environment.GetFullFileName("Data\\LeaderBoard.xml");

            IsFileExist();
        }

        public void AddPlayerToFile(string name, int score)
        {
            if (_isFirstInitialization)
            {
                XElement element = new XElement(new XElement("players", new XElement("player",
                new XElement("name", name),
                new XElement("score", score))));
                   document = new XDocument(element);

                   document.Save(_fullXmlFileName);
            }
            else
            {
                document = XDocument.Load(_fullXmlFileName);
                foreach (XElement item in document.Root.Elements())
                {
                    if (Int32.Parse(item.Element("score").Value) < score)
                    {
                           item.AddBeforeSelf(new XElement("player",
                                              new XElement("name", name),
                                              new XElement("score", score)));
                        document.Save(_fullXmlFileName);
                        return;

                    }
                }
                XElement element = new XElement("player",
                                   new XElement("name", name),
                                   new XElement("score", score));
                document.Root.Add(element);
                document.Save(_fullXmlFileName);
            }
        }

        public List<string> GetTopPlayers()
        {
            List<string> topPlayers = new List<string>();
            
            if (_isFirstInitialization)
            {
                topPlayers.Add("No one play this game yet!");
                return topPlayers;
            }

            document = XDocument.Load(_fullXmlFileName);
            var realQuantityOfPlayers = document.Root.Elements().Count();
            int quantityOfPlayers;

            if (realQuantityOfPlayers > 10)
            {
                quantityOfPlayers = 10;
            }
            else
            {
                quantityOfPlayers = realQuantityOfPlayers;
            }
            

            foreach (var item in document.Root.Elements())
            {
                if (quantityOfPlayers == 0)
                {
                    break;
                }
                topPlayers.Add(string.Format("{0} : {1}", item.Element("name").Value, item.Element("score").Value));
                quantityOfPlayers--;
            }

            return topPlayers;

        }

        public int GetTopPlayer() 
        {
            int topPlayerScore = 0;

            document = XDocument.Load(_fullXmlFileName);

            
            foreach (var item in document.Root.Elements())
            {
                topPlayerScore = Convert.ToInt32(item.Element("score").Value);
                break;
            }

            return topPlayerScore;
        }
         
        private void IsFileExist()
        {
            if (!File.Exists(_fullXmlFileName))
            {
                _isFirstInitialization = true;
            }

        }
    }
}

